package com.example.parking.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.parking.common.PageRequest;
import com.example.parking.common.Result;
import com.example.parking.dto.ParkingSpaceDTO;
import com.example.parking.entity.ParkingLot;
import com.example.parking.entity.ParkingSpace;
import com.example.parking.service.ParkingLotService;
import com.example.parking.service.ParkingSpaceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/parking-space")
public class ParkingSpaceController {

    @Autowired
    private ParkingSpaceService parkingSpaceService;

    @Autowired
    private ParkingLotService parkingLotService;

    @PostMapping
    public Result<?> create(@RequestBody ParkingSpace parkingSpace) {
        parkingSpaceService.save(parkingSpace);
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result<?> update(@PathVariable Long id, @RequestBody ParkingSpace parkingSpace) {
        parkingSpace.setId(id);
        parkingSpaceService.updateById(parkingSpace);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        parkingSpaceService.removeById(id);
        return Result.success();
    }

    @GetMapping("/listByLotId")
    public Result<?> listByLotId(@RequestParam Long lotId) {
        LambdaQueryWrapper<ParkingSpace> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ParkingSpace::getLotId, lotId);
        List<ParkingSpace> parkingSpaces = parkingSpaceService.list(wrapper);
        List<ParkingSpaceDTO> dtoList = parkingSpaces.stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
        return Result.success(dtoList);
    }

    @GetMapping("/{id}")
    public Result<ParkingSpaceDTO> getById(@PathVariable Long id) {
        ParkingSpace parkingSpace = parkingSpaceService.getById(id);
        if (parkingSpace == null) {
            return Result.error("车位不存在");
        }
        return Result.success(convertToDto(parkingSpace));
    }

    @GetMapping("/page")
    public Result<Page<ParkingSpaceDTO>> page(PageRequest pageRequest,
                                              @RequestParam(required = false) String spaceNumber,
                                              @RequestParam(required = false) Integer spaceType,
                                              @RequestParam(required = false) Integer status) {
        Page<ParkingSpace> page = new Page<>(pageRequest.getPageNum(), pageRequest.getPageSize());
        LambdaQueryWrapper<ParkingSpace> wrapper = new LambdaQueryWrapper<>();

        if (spaceNumber != null && !spaceNumber.isEmpty()) {
            wrapper.like(ParkingSpace::getSpaceNumber, spaceNumber);
        }
        if (spaceType != null) {
            wrapper.eq(ParkingSpace::getSpaceType, spaceType);
        }
        if (status != null) {
            wrapper.eq(ParkingSpace::getStatus, status);
        }
        wrapper.orderByDesc(ParkingSpace::getCreateTime);

        Page<ParkingSpace> rawPage = parkingSpaceService.page(page, wrapper);

        List<ParkingSpaceDTO> dtoList = rawPage.getRecords().stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());

        Page<ParkingSpaceDTO> dtoPage = new Page<>(rawPage.getCurrent(), rawPage.getSize(), rawPage.getTotal());
        dtoPage.setRecords(dtoList);

        return Result.success(dtoPage);
    }

    @GetMapping("/list")
    public Result<?> listAll() {
        List<ParkingSpace> parkingSpaces = parkingSpaceService.list();
        List<ParkingSpaceDTO> dtoList = parkingSpaces.stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
        return Result.success(dtoList);
    }

    private ParkingSpaceDTO convertToDto(ParkingSpace parkingSpace) {
        ParkingSpaceDTO dto = new ParkingSpaceDTO();
        BeanUtils.copyProperties(parkingSpace, dto);

        ParkingLot parkingLot = parkingLotService.getById(parkingSpace.getLotId());
        if (parkingLot != null) {
            dto.setLotName(parkingLot.getName());
        }
        return dto;
    }
} 